package com.example.administrator.myapplication.login;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;

import com.example.administrator.myapplication.MainActivity;
import com.example.administrator.myapplication.MyBaseActivity;
import com.example.administrator.myapplication.MyBaseApplication;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.model.User;
import com.example.administrator.myapplication.service.DBManager;
import com.example.administrator.myapplication.service.LoginJudge;

import com.example.administrator.myapplication.RegisterFragment;
import com.example.administrator.myapplication.service.WeChatDatabaseHelper;

public class AccountActivity extends MyBaseActivity {
    private static final String TAG = "AccountActivity";

    private FragmentManager fm;
    private Fragment fragment;
     private SQLiteDatabase sqLiteDatabase;
     private WeChatDatabaseHelper weChatDatabaseHelper;
    private DBManager dbManager;
    private User user;

   {
       if(fm==null) {
           fm = getSupportFragmentManager();
       }
        fragment = fm.findFragmentById(R.id.content_account);
    }

    /**
     * 检查用户是否登陆
     * 如果已经登陆，则跳转到主页面并销毁当前的Activity
     * 否则，什么也不做
     */
    @Override
    protected void onStart() {
        super.onStart();


        LoginJudge loginJudge = new LoginJudge(this);
          dbManager = new DBManager(this);
            loginJudge.selectAccountLandingStatus();

        if(loginJudge.selectAccountLandingStatus()!=null){
           user = dbManager.query(loginJudge.selectAccountLandingStatus());
            Intent intent = new Intent(AccountActivity.this, MainActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("userObject",user);
            intent.putExtra("account",bundle);
            startActivity(intent);
            AccountActivity.this.finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);
        if(fragment == null){
            fragment = new RegisterFragment();
            fm.beginTransaction().add(R.id.content_account, fragment).commit();
        }



    }

}
